package com.skynet.supervision.aop.uac;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Nonnull;
import javax.annotation.Resource;
import java.util.List;

@Configuration
@ComponentScan({"com.skynet.supervision.aop.uac.**"})
public class UacConfig implements WebMvcConfigurer {

    @Resource
    private UACClientProperties property;

    private final AuthorizationInterceptor authorizationInter;

    private final LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;

    public UacConfig(AuthorizationInterceptor authorizationInter, LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver) {
        this.authorizationInter = authorizationInter;
        this.loginUserHandlerMethodArgumentResolver = loginUserHandlerMethodArgumentResolver;
    }

    @Override
    public void addInterceptors(@Nonnull InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(this.authorizationInter);
        if (CollectionUtil.isNotEmpty(this.property.getExcludePathPatterns())) {
            registration.excludePathPatterns(this.property.getExcludePathPatterns());
        }
        //uacs client 内部集成的一些路径
        registration
                .excludePathPatterns(
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/v2/**",
                        "/swagger-ui.html/**")//swagger相关
                .excludePathPatterns(
                        "/user/login",
                        "/user/login/**",
                        "/users/login/",
                        "/users/login/**",
                        "/app/test/**",
                        "/app/user/login",
                        "/app/user/login/**",
                        "/test/*"

                )
                .excludePathPatterns( //静态资源
                        "/logo.png",
                        "/favicon.ico",
                        "/res/**",
                        "/js/**",
                        "/images/**",
                        "/css/**",
                        "/public/**",
                        "/statics/**",
                        "/static/**")
                .excludePathPatterns(//测试接口
                        "/laws/**",
                        "/app/learn/**",
                        "/talk/getRichtextByRecordId",
                        "/talk/uploadWord",
                        "/talk/upload"
//                        "/writes/upload"
                )
                .excludePathPatterns(//本地文件
                        "/api/file/**",
                        "file/**"
                );
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(loginUserHandlerMethodArgumentResolver);
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
    }
}
